(function(window, document, $) {
	
	
	var $selectByNameBtn,
	 $selectAllBtn,
	 $addBtn,
	 $updateBtn,
	 $deleteBtn,
	 $listNameBtn,
	 $previousBtn,
	 $nextBtn,
	 $listNameBtn,
	 $selectAllBtn,
	 id;
	
	$(function() {
		$selectByNameBtn = $("#selectByNameBtn").click(selectByName);
		$addBtn = $("#addBtn").click(addRetreat);
		$updateBtn = $("#updateBtn");
		$deleteBtn = $("#deleteBtn");
		$("#listNameBtn").click(listName);
		$previousBtn=$("#previousBtn").click(prePage);
		$nextBtn=$("#nextBtn");
		paintView();

		
	});
	
	window.addRetreat=function(){
		var nameId=($("#optionName").val());
		var id=nameId.substring(nameId.indexOf("&")+1);
		
		var url = "/api/retreat/add";
		$.post(url, {
			customerId:id,
			retreatType:$("#retreatTypeInput").val(),
			retreatTime:$("#retreatTimeInput").val(),
			retreatReason:$("#retreatReasonInput").val(),
			createTime:$("#createTimeInput").val()
		},
		function(resp) {
			if (resp.code === "ok") {
				
				setTimeout(window.location.reload(), 3000);
				toast("新增成功");
				retreatBed();
				deleteCustomer();
			}else{
				toast(resp.message)
			}
		});	
	}
	var retreatBed=function(){
		url="/api/bed/retreat?customerId="+id;
		$.get(url, function(resp) {
		});	
	}
	var deleteCustomer=function(){
		url="/api/customer/retreatCustomer?id="+id;
		$.get(url, function(resp) {
			
		});	
	}
	window.listName=function(){
		var url = "/api/customer/selectAll";
			$.get(url, function(resp) {
				if (resp.code === "ok") {
					console.log(resp.data);
					showListName(resp.data);
					
				}
				else{
					toast(resp.message)
				}
			});	
	};
	
	
	window.showListName=function(data){
		var html="";
		html+="<option value=''>--请选择--</option>";
		for (var i = 0; i < data.length; i++) {
			html+="<option value='"+data[i].id+"'>"+data[i].customerName+"&"+data[i].id+"</option>";
		}
		$("#optionName").html(html);
	}
	
	
	window.selectByName=function(){
		var url = "/api/retreat/list?pageNum=1&key="+$("#seleteInput").val();

		$.get(url, function(resp) {
			
			if (resp.code === "ok") {
				$("#tbody").empty();
				$("#divsys").empty();
				if(resp.data.list.length!==0){
					showData(resp.data.list);
					show(resp.data);
					
				}else{
					showNull();
				}
			}
			else{
				toast(resp.message)
			}
		});
	};
	window.paintView = function() {
		var url = "/api/retreat/list?pageNum=1";

		$.get(url, function(resp) {

			if (resp.code === "ok") {
				if(resp.data.list.length!==0){
				showData(resp.data.list);
				show(resp.data);
				
				}else{
					showNull();
				}
			}else{
				
				toast(resp.message)
			}
		});
	};
	window.showNull=function(){
		$("#divsys").html("<div style='text-align:center'>没有找到相关数据</div>");
	}
	window.show=function(data){
		var html="";
			html += '		<div class="col-md-12 text-right pd0 fmt20">                      '
			html += '			<nav aria-label="Page navigation">                            '
			html += '				<ul class="pagination">                                   '
			html += '					<li>                                                  '
			html += '						<button onclick="prePage('+data.pageNum+')" id="previousBtn" aria-label="Previous">        '
			html+=	"上一页"
			html += '						</button>                                         '
			html += '					</li>                                           '
			html += '					<li id="pageNumValue" >第'+(data.pageNum)+'页</li>                                    '
			html += '					<li>                                                  '
			html += '						<button onclick="nextPage('+(data.pageNum)+','+(data.pages)+')" id="nextBtn" aria-label="Next">           '
			html+="下一页"
			html += '						</button>                                      '
			html += '					</li>                                                 '
			html += '				</ul>                                                     '
			html += '			</nav>                                                        '
			html += '		</div>                                                            '
		$("#divsys").html(html)
	}
	window.showData = function(data) {
		var html = "";

		for (var i = 0; i < data.length; i++) {
			html += "<tr>";
			html += "<td>" + ((i + 1)) + "</td>";
			html += "<td>" + ((data[i].customerEntity.customerName)) + "</td>";
			html += "<td>" + ((data[i].customerEntity.customerSex)?"男":"女") + "</td>";
			html += "<td>" + ((data[i].customerEntity.customerAge)) + "</td>";
			html += "<td>" + ((data[i].customerEntity.recordId)) + "</td>";
			html += "<td>" +dateformat(new Date((data[i].customerEntity.checkinDate))) + "</td>";
			html += "<td>" +dateformat(new Date((data[i].retreatTime)))  + "</td>";
			html += "<td>" + (data[i].retreatType) + "</td>";
			html += "<td>" + (data[i].retreatReason) + "</td>";
			html+='<td><button onclick="f1('+data[i].id+')" type="submit"  class="btn btn-primary" data-toggle="modal" data-target=".myModal2">修改</button></td>'
			html+='<td><button type="button" onclick="deleteRetreat('+data[i].id+')" class="btn btn-danger">删除</button></td>'	
			html += "</tr>";
		}
		$("#tbody").html(html);
		
	};
	
	window.paintState = function(status) {
		switch (status) {
		case 0:
			return "女";
		case 1:
			return "男";
		}
	};
	window.dateformat=function(date){
		 date_value=date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();  
		   return date_value;
	}
	window.deleteRetreat=function(id){
		var url = "/api/retreat/delete?id="+id;
		$.get(url, function(resp) {
			if(resp.code=="ok"){
				toast("删除成功");
				setTimeout(window.location.reload(), 3000);
				
			}else{
				toast(resp.message)
			}
		});
	}
	 
	window.f1=function(oid){
		id=oid;
		
	}  
	 
		window.updateRetreat=function(){
		var url="/api/retreat/update";
		
		$.post(url,
			{
			id:id,
			retreatReason:$("#updateRetreatReasonInput").val(),
			retreatType:$("#updateRetreatTypeInput").val(),
			retreatTime:$("#updateRetreatTimeInput").val(),
			updateTime:$("#updateUpdateTimeInput").val()
			},
			function(resp){	
			if(resp.code=="ok"){
				toast("修改成功");
				setTimeout(window.location.reload(), 3000);
			}else{toast(resp.message)}
		})
	};
	window.prePage=function(page){
		if(page<=1){
			toast("首页");
			return;
		}
		page=page-1;
		 var url = "/api/retreat/list?pageNum="+page;
		
		$.get(url, function(resp) {
			if (resp.code === "ok") {
				
				$("#tbody").empty();
				showData(resp.data.list);
				show(resp.data);
			}
		}); 
	}
	window.nextPage=function(page,pages){
		if(page==pages){
			toast("尾页");
			return;
		}
		page=page+1;
		
		var url = "/api/retreat/list?pageNum="+page;
		$.get(url, function(resp) {
			if (resp.code === "ok") {
				$("#tbody").empty();
				showData(resp.data.list);
				show(resp.data);
			}
		}); 
	}
	
})(window, document, $);
